package com.yunkss.eftool.crypto.key;

import com.yunkss.eftool.crypto.params.CisAlgorithm;
import com.yunkss.eftool.crypto.params.SmProvider;

import javax.crypto.KeyGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;

/**
 * @author csx
 * @date 2022/2/18 8:28
 * @desc
 */

public class SM4KeyGenerator extends SmProvider {
    public static final int DEFAULT_KEY_SIZE = 128;

    public SM4KeyGenerator() {
    }

    private static byte[] generateSM4Key(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException {
        KeyGenerator kg = KeyGenerator.getInstance(CisAlgorithm.ALGORITHM_SM4_FOR_NAME.getName(), "BC");
        kg.init(keySize, new SecureRandom());
        return kg.generateKey().getEncoded();
    }

    public static String generateSM4Key() throws NoSuchProviderException, NoSuchAlgorithmException {
        byte[] sm4Key = generateSM4Key(128);
        return CisCodec.encodeToHexString(sm4Key);
    }
}
